const redis = require('redis');
const redisClient = redis.createClient(6379, '127.0.0.1');


redisClient.on("error", err => {
    console.log(err);
})

//全都tm用promise包装下。。
let sayncRedisSet = function (key, value) {
    return promise = new Promise((resolve) => {
        redisClient.set(key, value, (err) => {
            resolve(err == null ? true : err);
        })
    })
}

let sayncRedisGet = function (key) {
    return promise = new Promise((resolve) => {
        redisClient.get(key, (err, data) => {
            resolve(err == null ? data : err);
        })
    })
}

let asyncRedisIncr = function (key) {
    return promise = new Promise((resolve) => {
        redisClient.incr(key, (err, data) => {
            resolve(err == null ? data : err);
        })
    })
}

module.exports = {
    sayncRedisSet,
    sayncRedisGet,
    asyncRedisIncr
}